package com.hoongs.v1.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.hoongs.v1.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Repository
@TableName(value = "user")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = {"password","createTime", "updateTime"})
public class User {

    private static final long serialVersionUID = -40356785423868312L;

    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    private String avater;

    @NotBlank(message = "请输入用户名称")
    private String username;

    private String gender = "0"; // 性别信息 1:男 2:女 0:保密

    private String password;

    private String mobile;

    private String openid;         // 绑定的小程序id

    private String unionid;

    private String status = "0";         // 用户状态 0:不可用 1:正常

    private String role = "1";         // 用户角色 1: 普通用户 8: 骑手 9: 管理员

    private String isAuth = "0";         // 是否授权获取敏感信息 1: 是 0: 否

    private String summary;         // 商品简介

    @TableField(value = "default_address")
    private String defaultAddress;         // 默认地址

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;       // 发布时间

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;    // 更新时间


    public void from(WxUserInfo wxUserInfo) {
        this.username = wxUserInfo.getNickName();
        this.avater = wxUserInfo.getAvatarUrl();
        this.password = "";
        this.mobile = "";
        this.gender = wxUserInfo.getGender();
        this.openid = wxUserInfo.getOpenId();
        this.unionid = wxUserInfo.getUnionId();
    }

}
